import Sound = laya.media.Sound;
import SoundChannel = laya.media.SoundChannel;
import SoundManager = laya.media.SoundManager;

class PlaySound {
    private static playSound : PlaySound;
    static srcPlant = "sound/plant.mp3";
    static srcHarvest = "sound/harvest.mp3";
    static srcBg = ["sound/bgmOne.mp3","sound/bgmTwo.mp3"];
    private modeSound;

    private constructor(){
        // if (Laya.Browser.onWeiXin) {
        //     this.modeSound = WxSound.getWxSound();
        // }else{
        //     this.modeSound = H5Sound.getH5Sound();
        // }
        this.modeSound = H5Sound.getH5Sound();
    }

    public static getPlaySound() : PlaySound{
        if(!this.playSound) this.playSound = new PlaySound();
        return this.playSound;
    }

     //循环播放背景音乐
    playBg(index){
        this.modeSound.playBg(index);
    }

    //播放种植音乐
    playPlant(){
        this.modeSound.playPlant();
    }

    //播放收获音乐
    playHarvest(){
        this.modeSound.playHarvest();
    }
    
}

class H5Sound{
    private static h5Sound : H5Sound;
    private soundPlant : SoundChannel;
    private soundHarvest : SoundChannel;
    private musicBg = [];

    public static getH5Sound() : H5Sound{
        if(!this.h5Sound) this.h5Sound = new H5Sound();
        return this.h5Sound;
    }

    //循环播放背景音乐
    playBg(index){
        if(index>=1){
            this.musicBg[index] = SoundManager.playMusic(PlaySound.srcBg[index], 1, new laya.utils.Handler(this, this.playBg,[0]));
        }else{
            this.musicBg[index] = SoundManager.playMusic(PlaySound.srcBg[index], 1, new laya.utils.Handler(this, this.playBg,[index+1]));
        }
    }

    //播放种植音乐
    playPlant(){
        this.soundPlant = SoundManager.playSound(PlaySound.srcPlant, 1,null,null,0);
    }

    //播放收获音乐
    playHarvest(){
        this.soundHarvest = SoundManager.playSound(PlaySound.srcHarvest, 1,null,null,0);
    }

    getMusicBg(){
        return this.musicBg;
    }

    getSoundHarvest() : SoundChannel{
        return this.soundHarvest;
    }

    getSoundPlant() : SoundChannel{
        return this.soundPlant;
    }
}

/*class WxSound{
    private static wxSound : WxSound;
    private soundPlant;
    private soundHarvest;
    private musicBg = [];

    private constructor(){
        for(let i=0; i<PlaySound.srcBg.length; i++){
            this.musicBg[i] = wx.createInnerAudioContext();
            this.musicBg[i].src = PlaySound.srcBg[i];
            if(i == PlaySound.srcBg.length-1){
                this.musicBg[i].onEnded((res) => {
                        console.log('播放结束!');
                        this.musicBg[0].play();
                    });
                this.musicBg[i].onError((res) => {console.log('播放音频失败' + res);});
                this.musicBg[i].onStop((res) => {console.log('停止播放!' + res);});
            }else{
                this.musicBg[i].onEnded((res) => {
                        console.log('播放结束!');
                        this.musicBg[i+1].play();
                    });
                this.musicBg[i].onError((res) => {console.log('播放音频失败' + res);});
                this.musicBg[i].onStop((res) => {console.log('停止播放!' + res);});
            } 
        }

        this.soundPlant = wx.createInnerAudioContext();
        this.soundPlant.src = PlaySound.srcPlant;
        this.soundPlant.onEnded((res) => {console.log('播放结束!');});
        this.soundPlant.onError((res) => {console.log('播放音频失败' + res);});
        this.soundPlant.onStop((res) => {console.log('停止播放!' + res);});

        this.soundHarvest = wx.createInnerAudioContext();
        this.soundHarvest.src = PlaySound.srcHarvest
        this.soundHarvest.onEnded((res) => {console.log('播放结束!');});
        this.soundHarvest.onError((res) => {console.log('播放音频失败' + res);});
        this.soundHarvest.onStop((res) => {console.log('停止播放!' + res);});
    }

    public static getWxSound() : WxSound{
        if(!this.wxSound) this.wxSound = new WxSound();
        return this.wxSound;
    }

    //循环播放背景音乐
    playBg(index){
        this.musicBg[index].play();
    }

    //播放种植音乐
    playPlant(){
         this.soundPlant.play();
    }

    //播放收获音乐
    playHarvest(){
        this.soundHarvest.play();
    }
    
    getMusicBg(){
        return this.musicBg;
    }

    getSoundHarvest(){
        return this.soundHarvest;
    }

    getSoundPlant(){
        return this.soundPlant;
    }
}*/